﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace InfoPathTranslate
{

    class XsltActionMap
    {
        Hashtable evalueJs = new Hashtable();           //String, String
        Hashtable onChangeRelation = new Hashtable();   //String, ArrayList<String>
        Hashtable conditionJs = new Hashtable();        //String, String
        Hashtable affectAttributes = new Hashtable();   //String, ArrayList<String>
        private int domId = 1;
        private int conditionId = 1;

        public String GenerateDomId()
        {
            return "" + (this.domId++);
        }

        public String GenerateConditionId()
        {
            return "condition_" + (this.conditionId++);
        }

        public void SetEvalueJs(String domId, String js)
        {
            String oriJs = (String)evalueJs[domId];
            if (oriJs == null)
            {
                evalueJs.Add(domId, js);
            }
            else 
            {
                evalueJs[domId] = js;
            }
        }

        public String GetEvalueJs(String domId)
        {
            String js = (String)evalueJs[domId];
            if (js == null)
            {
                js = "";
            }
            return js;
        }

        public void AddOnChangeRelation(String changeName, String domId)
        {
            ArrayList list = (ArrayList)onChangeRelation[changeName];
            if (list == null)
            {
                list = new ArrayList();
                onChangeRelation.Add(changeName, list);
            }
            bool exists = false;
            for (int i = 0; i< list.Count;i++)
            {
                if (domId.Equals((String)list[i]))
                {
                    exists = true;
                    break;
                }
            }
            if (!exists){
                list.Add(domId);
            }
        }

        public String GetOnChangeJs(String changeName)
        {
            StringBuilder js = new StringBuilder();
            ArrayList list = (ArrayList)onChangeRelation[changeName];
            if (list == null)
            {
                return "";
            }
            for (int i = 0; i < list.Count; i++)
            {
                String domId = (String)list[i];
                if (i > 0)
                {
                    js.Append("\r\n");
                }
                js.Append("eval_").Append(domId).Append("();");
            }
            return js.ToString();
        }

        public bool NeedOnChange(String changeName)
        {
            ArrayList list = (ArrayList)this.onChangeRelation[changeName];
            if (list == null || list.Count == 0)
            {
                return false;
            }
            return true;
        }
        public void AddConditionJs(String conditionId, String js)
        {
            String ori = (String)this.conditionJs[conditionId];
            if (ori == null)
            {
                this.conditionJs.Add(conditionId, js);
            }
            else
            {
                this.conditionJs[conditionId] = js;
            }
        }

        public String GetConditionJs(String conditionId)
        {
            String ori = (String)this.conditionJs[conditionId];
            if (ori == null)
            {
                return "";
            }
            else
            {
                return (String)this.conditionJs[conditionId];
            }
        }

        public void AddAffectAttribute(String domId, String initAttrExp)
        {
            ArrayList list = (ArrayList)this.affectAttributes[domId];
            if (list == null)
            {
                list = new ArrayList();
                this.affectAttributes.Add(domId, list);
            }
            bool exists = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (initAttrExp.Equals((String)list[i]))
                {
                    exists = true;
                    break;
                }
            }
            if (!exists)
            {
                list.Add(initAttrExp);
            }
        }

        public ArrayList GetAffectAttribute(String domId)
        {
            StringBuilder js = new StringBuilder();
            return (ArrayList)onChangeRelation[domId];
        }

        public ArrayList GetConditionFunJs()
        {
            ArrayList list = new ArrayList();
            IDictionaryEnumerator i = this.conditionJs.GetEnumerator();
            while (i.MoveNext())
            {
                String js = "function " + (String)i.Key + "()\r\n{\r\n" + (String)i.Value + "\r\n}\r\n";
                list.Add(js);
            }
            return list;
        }

        public ArrayList GetEvalFunJs()
        {
            ArrayList list = new ArrayList();
            IDictionaryEnumerator i = this.evalueJs.GetEnumerator();
            while (i.MoveNext())
            {
                StringBuilder js = new StringBuilder();
                js.Append("function eval_").Append((String)i.Key).Append("()\r\n{\r\n");
                ArrayList attrs = (ArrayList)this.affectAttributes[(String)i.Key];
                if (attrs != null)
                {
                    for (int k = 0; k < attrs.Count; k++)
                    {
                        js.Append((String)attrs[k]).Append(";\r\n");
                    }
                }
                js.Append((String)i.Value).Append(";\r\n}\r\n");
                list.Add(js.ToString());
            }
            return list;
        }

        public ArrayList GetOnChangeFunJs()
        {
            ArrayList list = new ArrayList();
            IDictionaryEnumerator i = this.onChangeRelation.GetEnumerator();
            while (i.MoveNext())
            {
                StringBuilder js = new StringBuilder();
                js.Append("function onChange_").Append(((String)i.Key).Replace(':','_')).Append("()\r\n{\r\n");
                ArrayList relations = (ArrayList)i.Value;
                for (int k = 0; k < relations.Count; k++)
                {
                    String domId = (String)relations[k];
                    js.Append("eval_").Append(domId).Append("();\r\n");
                }
                js.Append("\r\n}\r\n");
                list.Add(js.ToString());
            }
            return list;
        }




        //for (int i = 0; i < myAffectList.Count; i++)
        //{
        //    String attr = (String)affectAttrs[i];
        //    String value = InfoPathXml.GetNodeAttr(domXmlNode, attr);
        //    if (value == null)
        //    {
        //        evalueJs.Append("removeAttribute('").Append(domId).Append("','").Append(attr).Append("');\r\n");
        //    }
        //    else
        //    {
        //        evalueJs.Append("setAttribute('").Append(domId).Append("','")
        //            .Append(attr).Append("','")
        //            .Append(value).Append("');\r\n");
        //    }
        //}
    }
}
